package com.lin.lostandfound.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.lostandfound.mapper.PickThingsMapper;
import com.lin.lostandfound.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lin.lostandfound.annotation.SystemServiceLog;
import com.lin.lostandfound.bean.PickThings;
import com.lin.lostandfound.service.IPickThingsService;

@Service("pickThingsService")
public class PickThingsService implements IPickThingsService {

	@Autowired
	private PickThingsMapper mapper;
	@Autowired
	private StringUtil util;

	// 添加
	@SystemServiceLog(description = "添加招领贴")
	@Override
	public boolean add(PickThings pickThings) {
		return mapper.insert(pickThings)!=-1;
	}

	// 批量修改状态
	@Override
	public boolean modifyStatus(String pickId, int status) {
		String[] id = pickId.split(",");
		PickThings things = new PickThings();
		for (int i = 0; i < id.length; i++) {
			things.setId(Integer.parseInt(id[i]));
			things.setStatus(status);
			mapper.updateById(things);
		}
		return true;
	}
	
	// 修改状态
	@Override
	public boolean modifyStatus(Integer pickId, int status) {

		PickThings things = new PickThings();
		things.setId(pickId);
		things.setStatus(status);

		return mapper.updateById(things) != -1;
	}

	// 批量删除
	@SystemServiceLog(description = "批量删除招领贴")
	@Override
	public boolean remove(String pickId) {
		String[] id = pickId.split(",");

		int[] lostsId = new int[id.length];
		for(int i = 0 ;i < id.length; i++){
			lostsId[i] = Integer.parseInt(id[i]);
		}
		int i = mapper.deleteBatchIds(Arrays.asList(lostsId));
		return i != -1;
	}
	
	// 查询所有统计数量
	@Override
	public long queryAllCount(String key) {

		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("thingsName",key);
			wrapper.or();
			wrapper.like("thingsType",key);
			wrapper.or();
			wrapper.like("userName",key);
		}
		int i = mapper.selectCount(wrapper);
		return i;
	}

	// 查询所有
	@Override
	public Map<String, Object> queryAll(String key, Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {
		Page<PickThings> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("thingsName",key);
			wrapper.or();
			wrapper.like("thingsType",key);
			wrapper.or();
			wrapper.like("userName",key);
		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("publishTime");
		}
		IPage<PickThings> page=mapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());

		return map;
	}

	// 查询所有（分页）
	@Override
	public List<PickThings> queryAll(Integer currentPage, Integer pageSize, boolean desc) {
		Page<PickThings> page_ = new Page<>(currentPage,pageSize);
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(desc){
			wrapper.orderByDesc("id");
		}

		IPage<PickThings> page=mapper.selectPage(page_,wrapper);

		return page.getRecords();
	}

	// 根据id查询
	@Override
	public PickThings queryById(long id) {
		return mapper.selectById(id);
	}

	// 根据用户id查询所有
	@Override
	public List<PickThings> queryAllByUid(long uid) {
		return null;
	}

	// 根据类型查询数量
	@Override
	public long queryByType(String type) {
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(util.notnull(type) && type.equals("all")){
			wrapper.eq("thingsType",type);

		}
		int i = mapper.selectCount(wrapper);
		return i;
	}
	
	// 根据用户id查询统计所有（分页）
	@Override
	public long queryAllByUidCount(Integer uid) {
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(uid!=null){
			wrapper.eq("uid",uid);

		}
		int i = mapper.selectCount(wrapper);
		return i;
	}

	// 根据用户id查询所有（分页）
	@Override
	public List<PickThings> queryAllByUid(Integer uid, Integer currentPage, Integer pageSize, boolean desc) {
		Page<PickThings> page_ = new Page<>(currentPage,pageSize);
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);

		if(uid!=null){
			wrapper.eq("uid",uid);

		}
		if(desc){
			wrapper.orderByDesc("id");

		}
		IPage<PickThings> page=mapper.selectPage(page_,wrapper);

		return page.getRecords();
	}

	// 根据关键字查询所有
	@Override
	public List<PickThings> queryAllByKeywords(String keywords) {
		return null;
	}
	
	// 根据关键字查询统计数量（分页）
	@Override
	public long queryAllByKeywordsCount(String keywords){
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(util.notnull(keywords)){
			wrapper.like("thingsName",keywords);
		}
		int i = mapper.selectCount(wrapper);
		return i;
	}

	// 根据关键字查询所有（分页）
	@Override
	public List<PickThings> queryAllByKeywords(String keywords, Integer currentPage, Integer pageSize, boolean desc) {
		Page<PickThings> page_ = new Page<>(currentPage,pageSize);
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(util.notnull(keywords)){
			wrapper.like("thingsName",keywords);
		}
		if(desc){
			wrapper.orderByDesc("id");
		}

		IPage<PickThings> page=mapper.selectPage(page_,wrapper);

		return page.getRecords();
	}

	// 根据类型查询所有（分页）
	@Override
	public List<PickThings> queryAllByThingsType(String type, Integer currentPage, Integer pageSize, boolean desc) {
		Page<PickThings> page_ = new Page<>(currentPage,pageSize);
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.ne("status",2);
		if(util.notnull(type)&&type.equals("all")){
			wrapper.like("thingsType",type);
		}
		if(desc){
			wrapper.orderByDesc("id");
		}

		IPage<PickThings> page=mapper.selectPage(page_,wrapper);

		return page.getRecords();
	}

	// 查询已归还
	@Override
	public long queryByReturnedCount() {
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.eq("status",1);
		return mapper.selectCount(wrapper);
	}

	@Override
	public List<PickThings> queryThingsBySuccess(int index) {
		QueryWrapper<PickThings> wrapper = new QueryWrapper<>();
		wrapper.eq("status",1);
		wrapper.orderByDesc("id");

		List<PickThings> things = mapper.selectList(wrapper);
		return things;
	}

	//查询一条记录（上一条、下一条）
	@Override
	public PickThings queryOneRecord(Long id, boolean isNext) {
		return mapper.next(id,isNext);
	}
}
